% # variable:
% # book, eroot, bookuuid, max_level, mode

% import lonote.translate
% g = lonote.translate.get_gettext_func()

% import uuid
% import datetime as DT
% from civa.tz import local_timezone

% def get_title(enote):
    % title = enote.findtext('./title')
    % return title
% end

% def get_path(enote, bookuuid):
    % path = '/site/{bookuuid}/{uuid}/'.format(bookuuid = bookuuid, uuid = enote.get('uuid'))
    % return path
% end

% now = DT.datetime.now(tz = local_timezone())

% time_format = '%Y-%m-%d'
% im_time_format = '%Y-%m-%dT%H:%M:%S.%f%z'

% def get_time(mtime):
    % desc = ''
    % delta = now - mtime

    % if delta.days == 0:
        % desc = g(' (Today)')
    % elif delta.days == 1:
        % desc = g(' (Yesterday)')
    % elif 2 <= delta.days <= 29:
        % desc = g(' ({} days ago)').format(delta.days)
    % elif 30 <= delta.days <= 364:
        % desc = g(' ({} months ago)').format(delta.days // 30)
    % else:
        % desc = g(' ({} years ago)').format(delta.days // 365)
    % end

    % return mtime.strftime(time_format) + desc
% end


% def get_mtime(enote):
    % mtime = DT.datetime.strptime(enote.findtext('modified_time'), im_time_format)
    % return get_time(mtime)
% end


% def get_ctime(enote):
    % ctime = DT.datetime.strptime(enote.findtext('created_time'), im_time_format)
    % return get_time(ctime)
%end


% def get_tail(book, enote):
    % tail = ''

    % allnotes = enote.findall('./note')

    % resource_list = book.resource_list(enote.get('uuid'))

    % if len(resource_list) > 0:
        % tail = ' <span class="res-notify" title="{}">'.format(g('Some resource in here'))
        % for res in resource_list:
            % if res['res_type'] == 'att':
                % tail += ' <img class="res-notify-icon" src="/static/icon/sidebar/att.png">'
                % break
            % end
        % end
        % for res in resource_list:
            % if res['res_type'] == 'emb':
                % tail += ' <img class="res-notify-icon" src="/static/icon/sidebar/emb.png">'
                % break
            % end
        % end
        % tail += ' </span> '
    % end

    % return tail
% end

% def multi_level_menu(enote, level):
    % def recu(enote, level):
        % if level < 0:
            % return
        % end

        % title = get_title(enote)

        % path = get_path(enote, bookuuid)
        % if mode == 'html':
            % path = path + '?mode=html'
        % elif mode == 'raw':
            % path = path + '?mode=raw'
        % end

        % name = uuid.uuid5(uuid.UUID(bookuuid), enote.get('uuid'))

        % esubnotes = enote.findall('./note');
        % modified_text = g('Modified: {}').format(get_mtime(enote))
        % # created_text = g('Created: {}').format(get_ctime(enote))

        % if len(esubnotes):
            <li class="ui-state-default"><input type="checkbox" name="{{name}}" id="{{name}}" /><label for="{{name}}"></label><a title="{{modified_text}}" href="{{path}}">{{title}}{{!get_tail(book, enote)}}</a>
        % else:
            <li class="leaf ui-state-default"><a title="{{modified_text}}" href="{{path}}">{{title}}{{!get_tail(book, enote)}}</a>
        % end

        % level = level -1
        % if len(esubnotes) and not (level < 0) :
            <ul class="sortable">
                %for esub in esubnotes:
                    %recu(esub, level)
                %end
            </ul>
        % end
        </li>
    % end

    % recu(enote, level)
% end

% multi_level_menu(eroot, max_level)
